





//
//  NWriteMsgViewController.m
//  nncloud
//
//  Created by 朵 on 2018/9/22.
//  Copyright © 2018年 youedata. All rights reserved.
//

#import "NWriteMsgViewController.h"
#import "UITextView+PlaceHolder.h"
#import "NStarPopView.h"
#import "NConfirmPopView.h"
#import "NFeedbackPopView.h"

@interface NWriteMsgViewController ()<SHAddImgViewDelegate,UITextViewDelegate>

//@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *wordLabel;

@property (nonatomic,strong)UITextView *textView;

@property (nonatomic, strong) NSArray *photos;

@property (nonatomic, strong) SHAddImgView *addImgView;

@property (nonatomic,strong)NStarPopView *starview;

@property (nonatomic,strong)NConfirmPopView *confirmView;

@property (nonatomic,strong)NFeedbackPopView *feedPopView;

@property (nonatomic,strong)UIButton *coverBtn;


@end

@implementation NWriteMsgViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.coverBtn.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.coverBtn.hidden = YES;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    NSString *title=self.isAutoReplay?@"编写回复内容":@"编写留言";
    [self.navigationItem setItemWithTitle:title textColor:kNavigationTitleColor fontSize:18 itemType:center];
    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 36, 36) title:@"提交" Image:nil selectedIMG:nil tartget:self action:@selector(submitAction)];
    
    [self coverView];
    
    [self addPhotoArea];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEditChanged:) name:UITextViewTextDidChangeNotification object:nil];
    [self addTextView];
}


- (void)addTextView {
    
    self.textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 20, ScreenWidth-20, 180)];
    self.textView.font = kFont15;
//    self.textView.backgroundColor = kMagentaColor;
    self.textView.placeholder =self.isAutoReplay?@"请输入您的答复内容(不多于140字)":@"请输入您的留言内容(不多于140字)";
    [self.view addSubview:self.textView];
}

//添加遮罩防止暴力点击
- (void)coverView {
    UIButton *cover = [UIButton buttonWithType:UIButtonTypeCustom];
    cover.frame = CGRectMake(ScreenWidth-60, 0, 50, 40);
    [cover setTitle:@"提交" forState:UIControlStateNormal];
    [cover setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cover.titleLabel.font = [UIFont systemFontOfSize:16];
    [cover addTarget:self action:@selector(submitAction) forControlEvents:UIControlEventTouchUpInside];
    cover.backgroundColor = [UIColor whiteColor];
    self.coverBtn = cover;
    cover.uxy_acceptEventInterval = 3.f;
    [self.navigationController.navigationBar addSubview:cover];
}

#pragma mark - textView delegate

- (void)textViewEditChanged:(NSNotification*)obj {
    UITextView *textView = self.textView;
    NSString *textStr = textView.text;
    NSInteger fontNum = 140 - textStr.length;
    fontNum = fontNum < 0 ? 0 : fontNum;
    self.wordLabel.text = [NSString stringWithFormat:@"%@",@(fontNum)];
    if (textStr.length > 140) {
        textView.text = [textStr substringToIndex:140];
    }
}


- (void)submitAction {
    YYLog(@"提交");
//    UIImage *image = [UIImage imageNamed:@"head.png"];
    //下面是iOS两种获取图片的方法，一种获取为jpeg，一种获取为png
    //转为jpeg上传可以让图片的大小压缩
//    NSMutableArray *photos = [NSMutableArray array];
//    [photos addObject:image];
    
    NSString *word = self.textView.text?self.textView.text:@"";
    if (word.length<1&&(self.photos.count<1)) {
        [AFToast showText:@"请完善您的反馈"];
        return;
    }
//    NSDictionary *dic = @{@"questionId":@"134",@"questionUserId":@"1",@"questionUserName":@"雷军",@"questionContent":word};
    
    if (!self.problemId||![NUserModel shareInstance].userInfoId) {
        [AFToast showText:@"参数不完整"];
        return;
    }
    NSString *questionUserName;
    
    //如果有科室名称的话传科室名称
    if (self.officeName.length>0) {
        questionUserName = self.officeName;
    }else{
        questionUserName = [NUserModel shareInstance].userInfoName;
    }
    
    
    //2018.12.28添加了参数  questioinFlag   0自行答复   1委办局答复 此处参数后续需要动态化
    NSDictionary *dic = @{@"questionId":self.problemId,@"questionUserId":[NUserModel shareInstance].userInfoId,@"questionUserName":questionUserName,@"questionContent":word,@"questioinFlag":@"0"};
    
    NSString *url=self.isAutoReplay?kAutoanswerBySelfUrl:kUploadImgUrl;
    
    [SVProgressHUD show];
    [NNetworkClient uploadImage:self.photos url:url parameters:dic success:^(id responseObject) {
        
        YYLog(@"%@",responseObject);
        NSString *msg = responseObject[@"msg"];
        if ([responseObject[@"code"] isEqualToString:@"0"]) {
            
            [AFToast showText:@"提交成功"];
            
        }else{
            [AFToast showText:msg];
        }
        
        WeakStament(ws);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [NUserModel shareInstance].isRefreshContentvView = YES;
            [SVProgressHUD dismiss];
            [ws.navigationController popViewControllerAnimated:YES];
            
        });
       
        
    } fail:^(NSError *error) {
        
        YYLog(@"%@",error);
        
    } delegater:nil];
    
    
}

///添加照片
- (void)addPhotoArea{
    
    SHAddImgView *addImgView = [[SHAddImgView alloc] initWithMaxColumn:4];
    addImgView.frame = CGRectMake(10, 200, ScreenWidth-20, 360);
    //    addImgView.frame = CGRectMake(0, 100, ScreenWidth, 360);
    addImgView.configModel = [SHConfigModel creatNormalConfig];
    addImgView.backgroundColor = kWhiteColor;
    addImgView.delegate = self;
    addImgView.ImgBlock = ^(NSArray *photos) {
        YYLog(@"%@",photos);
        self.photos = photos;
    };
    _addImgView = addImgView;
    [self.view addSubview:_addImgView];
}


#pragma mark - photo delegate
-(void)addImgIBActionImageSource:(NSArray<UIImage *> *)imageSource{
    YYLog(@"%@",imageSource);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
    
}

@end
